package com.sz.admin.vehicleschedule.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sz.admin.vehicleschedule.service.VehicleScheduleService;
import com.sz.admin.vehicleschedule.pojo.po.VehicleSchedule;
import com.sz.admin.vehicleschedule.mapper.VehicleScheduleMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.PageUtils;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.Utils;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.sz.admin.vehicleschedule.pojo.dto.VehicleScheduleCreateDTO;
import com.sz.admin.vehicleschedule.pojo.dto.VehicleScheduleUpdateDTO;
import com.sz.admin.vehicleschedule.pojo.dto.VehicleScheduleListDTO;
import com.sz.admin.vehicleschedule.pojo.dto.VehicleScheduleImportDTO;
import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.sz.core.util.FileUtils;
import com.sz.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.sz.admin.vehicleschedule.pojo.vo.VehicleScheduleVO;

/**
 * <p>
 * 排班时间表 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-08-30
 */
@Service
@RequiredArgsConstructor
public class VehicleScheduleServiceImpl extends ServiceImpl<VehicleScheduleMapper, VehicleSchedule> implements VehicleScheduleService {
    @Override
    public void create(VehicleScheduleCreateDTO dto){
        VehicleSchedule vehicleSchedule = BeanCopyUtils.copy(dto, VehicleSchedule.class);
        save(vehicleSchedule);
    }

    @Override
    public void update(VehicleScheduleUpdateDTO dto){
        VehicleSchedule vehicleSchedule = BeanCopyUtils.copy(dto, VehicleSchedule.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(VehicleSchedule::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(vehicleSchedule);
    }

    @Override
    public PageResult<VehicleScheduleVO> page(VehicleScheduleListDTO dto){
        Page<VehicleScheduleVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), VehicleScheduleVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<VehicleScheduleVO> list(VehicleScheduleListDTO dto){
        return listAs(buildQueryWrapper(dto), VehicleScheduleVO.class);
    }
    @Override
    public List<VehicleScheduleVO> listNop(VehicleScheduleListDTO dto){
        return listAs(buildQueryWrapper(dto), VehicleScheduleVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public VehicleScheduleVO detail(Object id){
        VehicleSchedule vehicleSchedule = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(vehicleSchedule);
        return BeanCopyUtils.copy(vehicleSchedule, VehicleScheduleVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<VehicleScheduleImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), VehicleScheduleImportDTO.class, true);
        List<VehicleScheduleImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(VehicleScheduleListDTO dto, HttpServletResponse response) {
        List<VehicleScheduleVO> list = list(dto);
        String fileName = "排班时间表模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "排班时间表", VehicleScheduleVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(VehicleScheduleListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(VehicleSchedule.class);
        if (Utils.isNotNull(dto.getTitle())) {
            wrapper.eq(VehicleSchedule::getTitle, dto.getTitle());
        }
        if (Utils.isNotNull(dto.getStartTimeStart()) && Utils.isNotNull(dto.getStartTimeEnd())) {
            wrapper.between(VehicleSchedule::getStartTime, dto.getStartTimeStart(), dto.getStartTimeEnd());
        }
        if (Utils.isNotNull(dto.getEndTimeStart()) && Utils.isNotNull(dto.getEndTimeEnd())) {
            wrapper.between(VehicleSchedule::getEndTime, dto.getEndTimeStart(), dto.getEndTimeEnd());
        }
        return wrapper;
    }
}